import { Model, DataTypes } from 'sequelize';
import sequelize from '@/config/database';
import Role from './role.model';
import Dept from './dept.model';

// 定义角色部门关联属性接口
interface RoleDeptAttributes {
  roleId: number;
  deptId: number;
}

// 角色部门关联模型类
class RoleDept extends Model<RoleDeptAttributes> implements RoleDeptAttributes {
  public roleId!: number;
  public deptId!: number;
}

RoleDept.init(
  {
    roleId: {
      type: DataTypes.BIGINT,
      primaryKey: true,
      field: 'role_id',
      references: {
        model: Role,
        key: 'role_id'
      }
    },
    deptId: {
      type: DataTypes.BIGINT,
      primaryKey: true,
      field: 'dept_id',
      references: {
        model: Dept,
        key: 'dept_id'
      }
    }
  },
  {
    sequelize,
    tableName: 'sys_role_dept',
    timestamps: false
  }
);

// 建立关联关系
Role.belongsToMany(Dept, {
  through: RoleDept,
  foreignKey: 'roleId',
  otherKey: 'deptId'
});

Dept.belongsToMany(Role, {
  through: RoleDept,
  foreignKey: 'deptId',
  otherKey: 'roleId'
});

export default RoleDept; 